<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Credentials\Ini;

class IniCredential
{
    use CreateTrait;
    use OptionsTrait;
    protected $filename;
    private static $hasLoaded;
    public function __construct($filename = "")
    {
        $this->filename = $filename ?: $this->getDefaultFile();
    }
    public function getDefaultFile()
    {
        return self::getHomeDirectory() . DIRECTORY_SEPARATOR . ".alibabacloud" . DIRECTORY_SEPARATOR . "credentials";
    }
    private static function getHomeDirectory()
    {
        if(getenv("HOME")) {
            return getenv("HOME");
        }
        return getenv("HOMEDRIVE") && getenv("HOMEPATH") ? getenv("HOMEDRIVE") . getenv("HOMEPATH") : NULL;
    }
    public static function forgetLoadedCredentialsFile()
    {
        self::$hasLoaded = [];
    }
    public function getFilename()
    {
        return $this->filename;
    }
    protected static function isNotEmpty(array $array, $key)
    {
        return isset($array[$key]) && !empty($array[$key]);
    }
    public function missingRequired($key, $clientName)
    {
        throw new \AlibabaCloud\Client\Exception\ClientException("Missing required '" . $key . "' option for '" . $clientName . "' in " . $this->getFilename(), \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
    }
    public function load()
    {
        if(isset(self::$hasLoaded[$this->filename])) {
            foreach (self::$hasLoaded[$this->filename] as $projectName => $client) {
                $client->name($projectName);
            }
            return self::$hasLoaded[$this->filename];
        } else {
            return $this->loadFile();
        }
    }
    private function loadFile()
    {
        if(!inOpenBasedir($this->filename)) {
            return [];
        }
        if(!is_readable($this->filename) || !is_file($this->filename)) {
            if($this->filename === $this->getDefaultFile()) {
                return [];
            }
            throw new \AlibabaCloud\Client\Exception\ClientException("Credential file is not readable: " . $this->getFilename(), \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
        }
        return $this->parseFile();
    }
    private function parseFile()
    {
        try {
            $file = parse_ini_file($this->filename, true);
            if(is_array($file) && $file !== []) {
                return $this->initClients($file);
            }
            throw new \AlibabaCloud\Client\Exception\ClientException("Format error: " . $this->getFilename(), \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL);
        } catch (\Exception $e) {
            throw new \AlibabaCloud\Client\Exception\ClientException($e->getMessage(), \AlibabaCloud\Client\SDK::INVALID_CREDENTIAL, $e);
        }
    }
    private function initClients($array)
    {
        foreach (array_change_key_case($array) as $clientName => $configures) {
            $configures = array_change_key_case($configures);
            $clientInstance = $this->createClient($clientName, $configures);
            if($clientInstance instanceof \AlibabaCloud\Client\Clients\Client) {
                self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
                self::setClientAttributes($configures, $clientInstance);
                self::setCert($configures, $clientInstance);
                self::setProxy($configures, $clientInstance);
            }
        }
        return isset(self::$hasLoaded[$this->filename]) ? self::$hasLoaded[$this->filename] : [];
    }
}

?>